import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ${USER}
 * Date: ${YEAR}-${MONTH}-${DAY}
 * Time: ${TIME}
 */
public class Main {
    public static void main(String[] args) throws SQLException {
        //创建数据资源库
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java5?characterEncoding=utf8&useSSl=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("19705004009");

        //与数据资源库建立连接,连接建立后进行后续的数据响应
        Connection connection = dataSource.getConnection();

        //构造sql语句
        String sql = "create table test1(id int,name varchar(10))";//sql语句中不写“”

        //String sql = "select * from test1";//查询语句
        //String sql = "insert into test1(id,name) values (?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,1);
        preparedStatement.setString(2,"李四");

        //发送给服务器
        int n = preparedStatement.executeUpdate();//返回值为影响到的行数
        System.out.println(n);
//        ResultSet resultSet = preparedStatement.executeQuery();//使用查询语句
//        while (resultSet.next()) {
//            System.out.println(resultSet.getInt("id"));
//            System.out.println(resultSet.getString("name"));
//        }

        //释放资源,先使用的后释放
        preparedStatement.close();
        connection.close();
    }
}